package com.yzr.common.thread.queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * ArrayBlockQueue四种应用场景
 */
public class ArrayBlockQueueDemo {
    // 队列的大小
    public static ArrayBlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(3);


    public static void main(String[] args) throws InterruptedException {
        test4();
    }

    /**
     * 阻塞队列抛出异常
     */
    public static void test1() {
        // 队列的大小
        ArrayBlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        // Exception in thread "main" java.lang.IllegalStateException: Queue full
        // 添加元素超出
        // System.out.println(blockingQueue.add("d"));

        // 移除元素
        System.out.println("==================================");
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        // 队列为空时remove->java.util.NoSuchElementException
        // System.out.println(blockingQueue.remove());
    }

    /**
     * 返回值校验是否添加成功
     */
    public static void test2() {
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        // 添加失败返回false
        // System.out.println(blockingQueue.offer("d"));

        // 检查队首元素
        // System.out.println(blockingQueue.element());
        System.out.println("===========================");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        // 获取失败返回null
        // System.out.println(blockingQueue.poll());
    }

    /**
     * 添加失败等待
     */
    public static void test3() throws InterruptedException {
        blockingQueue.put("a");
        blockingQueue.put("a");
        blockingQueue.put("a");
        // 添加元素超出队列大小会一直等待
        // blockingQueue.put("d");

        // 获取队首元素
        // System.out.println(blockingQueue.peek());
        System.out.println("===================");
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        // 队列元素为空时会一直等待
        // System.out.println(blockingQueue.take());
    }

    /**
     * 超时等待，设置时间
     */
    public static void test4() throws InterruptedException {
        System.out.println(blockingQueue.offer("a", 1, TimeUnit.SECONDS));
        System.out.println(blockingQueue.offer("b", 1, TimeUnit.SECONDS));
        System.out.println(blockingQueue.offer("c", 1, TimeUnit.SECONDS));
        // 添加元素超出队列大小，会根据设置的时间等待，随后退出等待，随后返回false
        // System.out.println(blockingQueue.offer("c", 1, TimeUnit.SECONDS));

        // 获取队首元素
        // System.out.println(blockingQueue.peek());
        System.out.println("========================================");
        System.out.println(blockingQueue.poll(1, TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll(1, TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll(1, TimeUnit.SECONDS));
        // 获取元素为空时返回null
        System.out.println(blockingQueue.poll(1, TimeUnit.SECONDS));


    }
}
